﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Bussiness;
using Utilities;
using Entity;


namespace UI
{
    public class Evacuation
    {
        private int _FireSource = -1;
        private int _CurrentGroupID = -1;
        private ProgressBar pb = null;
        private string msg = string.Empty;

        private Label msgLabel = null;        
        private SortedList croomList=null;
        private ClosestFacilityLayer cf = null;
        private EClassRoom cRoomOnFire=null;

        public int FireSource
        {
            get { return this._FireSource; }
            set { this._FireSource = value; }
        }
        public string Message
        {
            get { return this.msg; }
        }
        public ProgressBar ProcessBar
        {
            set { this.pb = value; }
        }
        public Evacuation(int fireSource)
        {
            this._FireSource = fireSource;
        }
        public Evacuation(int fireSource, ProgressBar pb,Label msgLabel)
        {
            this._FireSource = fireSource;
            this.pb = pb;
            this.msgLabel = msgLabel;            
        }
        public Evacuation(EClassRoom cr,ProgressBar pb,Label msgLabel)
        {
            this._FireSource = cr.CRCode;
            this.pb = pb;
            this.msgLabel = msgLabel;
            this.cRoomOnFire = cr;
        }
        
        public void excute()
        {
            SysConfigsManager cfm;
            int lastGroupID;
            CourseManager cm = new CourseManager();
            croomList = new SortedList();
            croomList = cm.GetCurrentCrWithID();
            cf = new ClosestFacilityLayer();
            cfm = new SysConfigsManager();
            lastGroupID = Convert.ToInt32(cfm.GetParaByName("LastGroupID").ToString());
            this.pb.Visible = true;
            if (croomList.Count > 0)
            {
                IList keyList = croomList.GetKeyList();
                IList valueList = croomList.GetValueList();  
                //首先疏散着火位置
                int currentGID=lastGroupID+1;
                this._CurrentGroupID = currentGID;
                for (int i = croomList.Count - 1; i >= 0; i--)
                {
                    int crCode = Convert.ToInt32(keyList[i]);
                    int courseID = Convert.ToInt32(valueList[i]);
                    if (crCode == this._FireSource)//找到着火的教室
                    {
                        cf.SolveRoute(this._FireSource, currentGID,courseID, null);//着火的教室，不设置火灾障碍，                        break;
                    }
                }                
                
                //按楼层高到低疏散其他各个教室
                for (int i = croomList.Count - 1, j = 0; i >= 0; i--, j++)
                {
                    int crCode = Convert.ToInt32(keyList[i]);
                    int courseID=Convert.ToInt32(valueList[i]);
                    if (crCode != this._FireSource)
                    {
                        cf.SolveRoute(crCode, currentGID,courseID,this._FireSource);//其他的教室，当路径遇到火灾障碍的时候，绕道而行

                    }
                    else
                    {
                        continue;
                    }
                    //显示进度条
                    if (pb != null)
                    {
                        this.pb.Value = 100 * (j + 1) / croomList.Count;
                    }
                    if (msgLabel != null)
                    {
                        this.msgLabel.Text = "正在发送知明1-" + crCode + "教室的最优疏散路径……";
                    }
                }
                cfm.UpdateParaByName("LastGroupID", currentGID.ToString());                

                this.pb.Visible = false;
                this.msgLabel.Text = "疏散路径发送完成";
                MessageBox.Show("疏散路径已发送完毕，共生成"+croomList.Count+"条路径");
            }
            else
            {
                this.msg = "当前不属于上课时间，没有需要疏散的人群！";
                this.msgLabel.Text = this.msg ;
            }
            
        }
        public bool addFire()
        {
            bool isOK=false;
            if (_FireSource != -1 && _CurrentGroupID != -1)
            {
                FireGroupManager fg = new FireGroupManager();
                isOK=fg.AddFire(this._CurrentGroupID, this._FireSource, "严重");
            }
            return isOK;
        }
    }
}
